//下单和买涨买跌的弹出
var publicbody=avalon.define({
        $id: "pulicorder",
        up:'',//涨多少
        ups:'',//涨多少
        last_price:'',//最后一次价格
        sell_price:'',//卖出价
        buy_price:'',//买入价
        pulicvisible:'0',//购买页和主页面的切换
        clickbuynow:'0',//选择的是买涨还是买跌
        buyclicknow:'',//购买页显示的价格
        buyclickname:heyuenameip,//当前合约名称
        buyclickid:"{$option['id']}",//合约id
        timelist:[60,300,900,1800,3600],//时间列表
        timelv:[0.76,0.78,0.80,0.82,0.85],//利率列表
        timelistclass:1,//选中的时间
        timelistshow:'0',//显示时间列表
        monylist:[50,100,200,500,1000,2000,5000],//金额列表
        monylistclass:1,//选中的金额
        monylistshow:'0',//显示金额列表
        shouyimony:'176',//显示收益
        msgs:'',//提示弹窗
        msgshow:'0',//提示弹窗的显示
        msgstatu:null,//成功提示
        page:'1',//结算页码
        setarr:[],//结算数组
        pubarr:[],//持仓数组
        chagemongy:function(tey,show,time,mongy){
        	if(tey=='time'){
        		if(show=='yes'){
        			publicbody.timelistclass=time;
        			publicbody.timelistshow=0;
        			publicbody.shouyimony=publicbody.monylist[publicbody.monylistclass]+publicbody.monylist[publicbody.monylistclass]*publicbody.timelv[publicbody.timelistclass].toFixed(2);
        		}else if(show=='no'){
        			publicbody.timelistclass=time;
        			publicbody.shouyimony=publicbody.monylist[publicbody.monylistclass]+publicbody.monylist[publicbody.monylistclass]*publicbody.timelv[publicbody.timelistclass].toFixed(2);
        		}
        	}else if(tey=='mony'){
        		if(show=='yes'){
        			publicbody.monylistclass=mongy;
        			publicbody.monylistshow=0;
        			publicbody.shouyimony=publicbody.monylist[publicbody.monylistclass]+publicbody.monylist[publicbody.monylistclass]*publicbody.timelv[publicbody.timelistclass].toFixed(2);
        		}else if(show=='no'){
        			publicbody.monylistclass=mongy;
        			publicbody.shouyimony=publicbody.monylist[publicbody.monylistclass]+publicbody.monylist[publicbody.monylistclass]*publicbody.timelv[publicbody.timelistclass].toFixed(2);
        		}
        	}
        },clickbuy:function(e){
        	//显示购买页面并记录买涨或买跌已及显示的价格
        	publicbody.pulicvisible='1';
        	publicbody.clickbuynow=e;
        },buyhide:function(){
        	//关闭购买页面清除买涨或买跌已及显示的价格
        	publicbody.pulicvisible='0';
        	publicbody.clickbuynow='';
        	
        },timeshow:function(e){
        	//span切换
        	publicbody.timelistclass=e;
        },msgclick:function(){
        	publicbody.msgshow=0;publicbody.msgs='';
        	if(publicbody.msgstatu==1){
        		publicbody.pulicvisible=0;
        		hasAJAX();
        	};
        	console.log('msggo')
        },clickgo:function(){
			$.ajax({
				type:"post",
				data:{
					option_id:publicbody.buyclickid,
					direction:publicbody.clickbuynow,
					duration:publicbody.timelist[publicbody.timelistclass],
					invest:publicbody.monylist[publicbody.monylistclass],
					yield_rate:publicbody.shouyimony
				},
				url:urli,
				//async:true,
				success:function(data){
					if(data['status']==1){
						publicbody.msgs=data['info'];publicbody.msgshow='1';
						var dl=parseFloat($('.pub-yuen').html())- parseFloat(publicbody.monylist[publicbody.monylistclass]);
						$('.pub-yuen').html(dl.toFixed(2));
					}else{
						publicbody.msgs=data['info'];publicbody.msgshow='1';
					}
					publicbody.msgstatu=data['status'];
				},
				error:function(e){
					console.log('buy'+e)
				}
			});//发送
        }
    })
onloads()//加载股价
function onloads(){
	setInterval(function(){
		var timestamp = Date.parse(new Date());
		url = urlg+'/_'+timestamp;
		$.ajax({
			type:"post",
			url:url,
			dataType:"json",
			async:true,
			success:function(datas){
				publicbody.up=datas.up;//涨多少
				publicbody.ups=datas.ups;//涨百分比
				publicbody.last_price=datas.last_price;//当前股价
				publicbody.sell_price=datas.sell_price;//卖出股价
				publicbody.buy_price=datas.buy_price;//买入股价
				if(publicbody.clickbuynow=='0'){
					publicbody.buyclicknow=datas.buy_price;
				}else if(publicbody.clickbuynow=='1'){
					publicbody.buyclicknow=datas.sell_price;
				}
			}
		})

	},5000)
}
//期权顶部切换
var toptabs=avalon.define({
        $id: "toptab",
        toptabtoggle: false,
        toptablos:function(){
        	toptabs.toptabtoggle=(toptabs.toptabtoggle==false) ? true : false;
        }
    })
//tab列表切换
var contertabs=avalon.define({
        $id: "contertab",
        visibili:'0',
        changelist:function(e){
        	//列表的切换
        	contertabs.visibili=e;
        }
    })


//tab列表切换
//setajax();
function setajax(){
	//加载结算
	$.ajax({
		type:"post",
		url:urlj,
		dataType:"json",
		data:[publicbody.page],
		async:true,
		success:function(datas){
			console.log(publicbody.pubarr+'/加载结算')
	},error:function(e){
		console.log(e+'/加载结算error')
	}
	})
}

//加载持仓
hasAJAX();
function hasAJAX(){
    $.ajax({
		type:"post",url:urlc,dataType:'json',async:true,
		success:function(data){
			$('.pub-order-list').eq(1).html("");
			var kle='';
			$.map(data,function(val,i){
				var classx=(data[i]["direction"]==1)?'pub-redcolor':'pub-bluecolor';
				kle+='<div class="order-list-gro clearfix order-lists" id='+data[i]["id"]+'>'+
								'<p class="pub-title"><span class="title-name">'+data[i]["option_name"]+'</span><span class="title-date" cur-time='+data[i]['cur_time']+'  data-time = '+data[i]['shen']+' data-over='+data[i]["duration"]+'><span>'+data[i]["format_start_time"]+'</span></span></p>'+
								'<div class="order-square clearfix">'+
									'<dl>'+
										'<dd>'+
											'<div class="square-info">'+
												'<p data-direction='+data[i]["direction"]+' class='+classx+'>'+data[i]["direction_str"]+'</p>'+
												'<p>金额<span>'+data[i]["invest"]+'</span>元</p>'+
												'<p>收益<span>'+data[i]["yield_rate"]*100+'</span>%</p>'+
												'<p>剩余'+
													'<div class="elapsed" data-time='+data[i]["duration"]+'>'+data[i]["shen"]+'</div>'+
												'</p>'+
											'</div>'+
										'</dd>'+
										'<dd>'+
											'<div class="square-profit">'+
												'<p>盈亏<span>'+data[i]['profit']+'</span>元</p>'+
												'<p>执行价<span>'+data[i]["buy_price"]+'</span></p>'+
												'<p>当前价<span>'+publicbody.last_price+'</span></p>'+
													'<div class="percent">'+
														'<div class="pbar"></div>'+
														'<b></b>'+
													'</div>'+
											'</div>'+
										'</dd>'+
										'<dd>'+
											'<a href="#" class="square-btn-doing">进行中</a>'+
										'</dd>'+
									'</dl>'+
								'</div>'+
							'</div>';	
			})
			$('.pub-order-list').eq(1).html(kle);
			if(data.length>0){
				xsetInterval();
			};
		}
	});
};
function xsetInterval(){
	$('.order-lists').each(function(){
		var self=this;
		var shen=parseInt($(this).find('.pub-title').find('.title-date').attr('data-time')),
		cur_time=parseInt($(this).find('.pub-title').find('.title-date').attr('cur-time')),
		go=parseInt($(this).find('.pub-title').find('.title-date').attr('data-over'));
		anmin(self,go,shen);
		Refresh()
	});
}
function anmin(self,going,shen){
var jindu	 =$(self).find('.order-square').find('dd').eq(1).find('.square-profit').find('.percent'),
	jindutext=$(self).find('.order-square').find('dd').eq(1).find('.square-profit').find('.pbar'),
	jindub   =$(self).find('.order-square').find('dd').eq(1).find('.square-profit').find('b'),
	jindus   =$(self).find('.order-square').find('dd').eq(0).find('.square-info').find('.elapsed');
var vInterval = setInterval(function(){
		if(shen<0){
			shen = 0;
			clearInterval(vInterval);
			$(self).remove();
		}
		jindus.html(shen+'秒');
		var pic=parseInt((going-shen)/going*100);
		jindutext.css('width',pic+'%');
		jindub.html(pic+'%');
		shen--;
	},1000)
};
//结算AJAX
overAJAX();
function overAJAX(){ 
	var url=urlj;
	var page=1;
	$.ajax({
		type:"post",
		dataType:'json',
		url:url,
		async:true,
		success:function(data){
			if(data.length==0){
				$(".jiesuanas").text("你还没有交易。。。");
				return;
			}
			$('.pub-order-list').eq(2).find('div').eq(0).find('.order-list-gro').remove();
			var olps='';
			afsp(olps,data,page);
		}
	});
};
$('.jiesuanas').click(function(){
    var a1=$('.pub-order-list').eq(2).find('div').eq(0).html();//原来的页面
	var url=urlj;
	var page=$('.pub-order-list').eq(2).find('div').eq(0).attr("data-page");
	page=parseInt(page)+1;
	$.ajax({
		type:"post",
		data:{page:page},
		dataType:'json',
		url:url,
		async:true,
		success:function(data){
			if(data.length==0){
				$(".jiesuanas").text("没有更多了...");
				return;
			}
			$('.pub-order-list').eq(2).find('div').eq(0).find('div').remove();
			var olspd=a1;
			afsp(olspd,data,page);
		}
	});
})

//拼接
function afsp(olps,data,page){
	$.map(data, function(val,i) {
		olps+='<div class="order-list-gro clearfix ">'+
							'<p class="pub-title"><span class="title-name">'+data[i]['option_name']+'</span><span class="title-date" >开仓'+data[i]['format_start_time']+'</span></p>'+
							'<div class="order-square clearfix">'+
								'<dl>'+
									'<dd>'+
										'<div class="square-info">'+
											'<p>'+data[i]['direction_str']+'</p>'+
											'<p>金额'+data[i]['invest']+'元</p>'+
											'<p>收益'+data[i]['yield_rate']*100+'%  </p>'+
											'<p>时长'+data[i]['duration']+'秒  </p>'+
										'</div>'+
									'</dd>'+
									'<dd>'+
										'<div class="square-profit">'+
											'<p >盈亏'+data[i]['profit']+'元</p>'+
											'<p>执行价'+data[i]['buy_price']+'</p>'+
											'<p>到期价'+data[i]['sell_price']+'</p>'+
											'<p>到期时间'+data[i]['format_end_time']+'</p>'+
										'</div>'+
									'</dd>'+
									'<dd>'+
										'<a href="#" class="square-btn">结算成功</a>'+
									'</dd>'+
								'</dl>'+
							'</div>'+
						'</div>';
	});
	$('.pub-order-list').eq(2).find('div').eq(0).html(olps);
	$('.pub-order-list').eq(2).find('div').eq(0).attr("data-page",page);
}
function Refresh(){
var yldq=setInterval(function(){
	var currentt=0;//总盈亏
	$('.order-lists').each(function(){
		var	now=parseFloat($('.stock-fund').html()),
			findp=$(this).find('.order-square').find('dd').eq(1).find('.square-profit').find('p'),//当前价格
			dangqianjia=parseInt(findp.eq(1).find('span').html()),//买入价格
			zijin=parseInt($(this).find('.order-square').find('dd').eq(0).find('.square-info').find('p').eq(1).find('span').html()),//投入资金
			profit=parseInt($(this).find('.order-square').find('dd').eq(0).find('.square-info').find('p').eq(2).find('span').html()),//收益率
			direction=$(this).find('.order-square').find('dd').eq(0).find('.square-info').find('p').eq(0).attr('data-direction');//方向
//		console.log('当前价格/'+now+'*** 买入价格/'+dangqianjia+'*** 收益率/'+profit+'*** 方向/'+direction);
		findp.eq(2).find('span').html(now);//当前价
		if(direction=='1'){
			$(this).find('.order-square').find('dd').eq(0).find('.square-info').find('p').eq(0).removeClass('pub-bluecolor').addClass('pub-redcolor');
			var linepic=now-dangqianjia;
			if(linepic>0){
				findp.eq(0).removeClass('pub-bluecolor').addClass('pub-redcolor');//盈亏537
 				findp.eq(0).find('span').html('+'+(zijin*profit/100+zijin));
 				currentt=currentt+zijin*profit/100+zijin;
			}else if(linepic<0){
				findp.eq(0).removeClass('pub-redcolor').addClass('pub-bluecolor');//盈亏537
 				findp.eq(0).find('span').html('-'+zijin);
 				currentt=currentt-zijin;
			}else if(linepic=0){
				findp.eq(0).removeClass('pub-redcolor').addClass('pub-bluecolor');//盈亏537
 				findp.eq(0).find('span').html('0');
 				currentt=currentt;
			}
		}else if(direction=='0'){
			$(this).find('.order-square').find('dd').eq(0).find('.square-info').find('p').eq(0).removeClass('pub-redcolor').addClass('pub-bluecolor');
			var linepic=dangqianjia-now;
			if(linepic>0){
				findp.eq(0).removeClass('pub-bluecolor').addClass('pub-redcolor');//盈亏537
 				findp.eq(0).find('span').html('+'+(zijin*profit/100+zijin));
 				currentt=currentt+zijin*profit/100+zijin;
			}else if(linepic<0){
				findp.eq(0).removeClass('pub-redcolor').addClass('pub-bluecolor');//盈亏537
 				findp.eq(0).find('span').html('-'+zijin);
 				currentt=currentt-zijin;
			}else if(linepic=0){
				findp.eq(0).removeClass('pub-redcolor').addClass('pub-bluecolor');//盈亏537
 				findp.eq(0).find('span').html('0');
 				currentt=currentt;
			}
		};
	});
	if(currentt>0){
		$('.funding').removeClass('pub-bluecolor').addClass('pub-redcolor');
	}else{
		$('.funding').removeClass('pub-redcolor').addClass('pub-bluecolor');
	}
	$('.funding').html(currentt.toFixed(2)+'元');
 },1000)
}